exports.bargain_home = function (req, res) {

    //获取restful参数
    var restfulParams = req.routeInfo.restfulParams;

    //最终结果
    var resultData = {};
    var fields = 1;
    var activityCommentData = 0;

    var skip = restfulParams.skip == undefined ? 0 : restfulParams.skip;

    var limit = restfulParams.limit == undefined ? config.publicConfig.LIMIT : restfulParams.limit;

    //任务流创建
    async.waterfall([
            //获取砍价活动参与信息
            //获取砍价活动信息
            function (cb) {

                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.search_organization_activity_info + "/oa_id/" + restfulParams.oa_id,
                        method: 'GET',
                    },
                    function (err, data) {
                        //判断是否有错误信息
                        if (err) {
                            return cb(err, data);
                        }

                        var activity = data.data;
                        if(activity.length>0){
                            resultData.activityData = data.data[0];
                        }else{
                            resultData.activityData = [];
                        }
                        cb();
                    }
                );


            },

            //查询砍价活动的扩展信息
            function (cb) {

                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.searchBargainActivityConfig + "/bc_oaid/" + restfulParams.oa_id,
                        method: 'GET',
                    },
                    function (err, data) {
                        //console.log(data);
                        //判断是否有错误信息
                        if (err) {
                            return cb(err, data);
                        }
                        resultData.activityConfigData = data.data[0];

                        cb();
                    }
                );

            },

            //查看砍价活动内容
            function (cb) {

                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.searchBargainActivityContent + "/bc_oaid/" + restfulParams.oa_id,
                        method: 'GET',
                    },
                    function (err, data) {
                        //console.log(data);
                        //判断是否有错误信息
                        if (err) {
                            return cb(err, data);
                        }
                        resultData.activityContentData = data.data;

                        cb();
                    }
                );

            },

            //获取荣誉榜前10
            function (cb) {

                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.searchBargainJoinUserInfo + "/bj_oaid/" + restfulParams.oa_id + "/sort_order/bj_get_cut_price-desc/skip/0/limit/10",
                        method: 'GET',
                    },
                    function (err, data) {
                        //console.log(data);
                        //判断是否有错误信息
                        if (err) {
                            return cb(err, data);
                        }

                        resultData.rankingData = data.data;

                        cb();

                    }
                );

            },
            function (cb) {

                var tasks = [];

                //循环节点列表
                resultData.rankingData.forEach(function (item, i) {
                    //创建每一个插入任务
                    tasks.push(function (cb) {

                        item.now_price = parseFloat(parseFloat(item.bc_most_price) - parseFloat(item.bj_get_cut_price)).toFixed(2);

                        cb();

                    });

                });

                async.waterfall(tasks, cb);


            },
            //获取参加活动的总人数
            function (cb) {

                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.searchBargainJoin + "/count/1/bj_oaid/" + restfulParams.oa_id,
                        method: 'GET',
                    },
                    function (err, data) {
                        //console.log(data);
                        //判断是否有错误信息
                        if (err) {
                            return cb(err, data);
                        }

                        resultData.joinCount = data.count;

                        cb();

                    }
                );

            },
            //浏览数加一
            function (cb) {

                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.browse_count_setInc,
                        method: 'POST',
                        data: {
                            oid: restfulParams.oa_id,
                        }
                    },
                    function (err, data) {
                        //判断是否有错误信息
                        if (err) {
                            return cb(err, data);
                        }

                        resultData.isaddone = true;

                        cb();
                    }
                );
            },
            //判断用户是否关注过这个机构
            function (cb) {

                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.userSearchCollect + "/is_delete/0/uc_type/1/uc_fid/" + restfulParams.oa_id + "/uc_uid/" + restfulParams.u_id,
                        method: 'GET',
                    },
                    function (err, data) {
                        // //console.log(data);
                        //判断是否有错误信息
                        if (err) {
                            return cb(err, data);
                        }
                        if (data.data.length > 0) {
                            resultData.isCollect = 1;
                        } else {
                            resultData.isCollect = 0;
                        }
                        cb();

                    }
                );

            },

            //获取我的砍价信息
            function (cb) {

                ////console.log(CORE_APIConfig.coreAPI.searchBargainJoinUserInfo + "/bj_uid/" + restfulParams.u_id + "/bj_oaid/" + restfulParams.oa_id)

                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.searchBargainJoinUserInfo + "/bj_is_complete/0/bj_uid/" + restfulParams.u_id + "/bj_oaid/" + restfulParams.oa_id,
                        method: 'GET',
                    },
                    function (err, data) {
                        //console.log(data);
                        //判断是否有错误信息
                        if (err) {
                            return cb(err, data);
                        }


                        //判断该用户是否参与砍价活动了
                        if (data.data.length <= 0) {
                            fields  = 0;
                            resultData.isParticipate = false;
                            cb();
                           // return RES.response(res, true, resultData);
                        } else {

                            resultData.joinData = data.data[0];
                            resultData.isParticipate = true;
                            cb();
                        }

                    }
                );
            },
            //讨论区
            function (cb) {
                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.activity_comment_info + "/oac_is_reply/0/oac_oaid/" + restfulParams.oa_id + "/skip/" + skip + "/limit/" + limit,
                        method: 'POST',
                        data: {
                            fields: ["oac_id", "oac_uid", "oac_content", "u_nickname", "u_header_url", "oac_zan_count", "oac_comment_time"].join(",")
                        }
                    },
                    function (err, data) {
                        //判断是否有错误信息
                        if (err) {
                            return cb(err, data);
                        }
                        activityCommentData = data.data;

                        resultData.activityCommentData = activityCommentData;

                        cb();
                    }
                );

            },
            function (cb) {

                //创建添加任务
                var tasks = [];

                //循环节点列表
                activityCommentData.forEach(function (item, i) {

                    //创建每一个插入任务
                    tasks.push(function (cb) {

                        if (activityCommentData.length > 0) {
                            // if(item.oac_uid>0){
                            R.SEND_HTTP(
                                req,
                                {
                                    url: CORE_APIConfig.coreAPI.activity_comment_info + "/count/1/oac_oaid/" + restfulParams.oa_id + "/oac_fid/" + item.oac_id,
                                    method: 'POST',
                                    data: {
                                        fields: ["oac_content", "u_nickname"].join(",")
                                    }
                                },
                                function (err, data) {

                                    //console.log(data);

                                    item.reply = data.data;
                                        item.replyCount = data.count;

                                    cb();
                                }
                            );
                            /*}else{
                                cb();
                            }*/
                        } else {
                            cb();
                        }

                    });

                });

                async.waterfall(tasks, cb);

            },
            //判断该砍价活动用户有没有有未支付的订单（如有，那么返回订单id）
            function(cb){
                    R.SEND_HTTP(

                    req,
                    {
                        url: CORE_APIConfig.coreAPI.search_order + "/o_type/0/o_pay_status/0/o_activity_type/2/o_uid/" + restfulParams.u_id + "/o_aoc_id/" +restfulParams.oa_id,
                        method: 'GET',
                        data: {
                            fields: ["o_id"].join(",")
                        }
                    },
                    function (err, data) {

                        //判断是否有错误信息
                        if (err) {
                            return cb(err, data);
                        }
                        if(data.data.length>0){
                            resultData.is_not_pay_order = data.data[0].o_id;
                        }else{
                            resultData.is_not_pay_order = 0;
                        }
                        cb();
                    }
                );
            },

            //相关数据计算
            function (cb) {
                if(fields == 1){
                //帮砍价的好友人数
                resultData.helpCount = resultData.joinData.bj_get_cut_count;

                //已经砍掉的钱数
                resultData.cutTotalMoney = resultData.joinData.bj_get_cut_price;

                //当前的价格
                resultData.nowMoney = parseFloat( parseFloat(resultData.activityConfigData.bc_most_price) - parseFloat(resultData.cutTotalMoney)).toFixed(2);

                //目标价
                resultData.targetMoney = resultData.activityConfigData.bc_least_price;

                //原价
                resultData.oldMoney = resultData.activityConfigData.bc_most_price;

                //是否下单
                resultData.is_order = resultData.joinData.bj_is_order;
                cb();

                }else{
                    cb();
                }
            }


        ], function (err, result) {

            //返回最终拼接结果
            RES.response(res, true, resultData);

        }
    );

};